package com.example.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.mapper.HeadlineMapper;
import com.example.pojo.Headline;
import com.example.pojo.vo.PortalVo;
import com.example.service.HeadlineService;
import com.example.utils.JwtHelper;
import com.example.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author wqe
 * @description 针对表【news_headline】的数据库操作Service实现
 * @createDate 2024-03-14 11:32:59
 */
@Service
public class HeadlineServiceImpl extends ServiceImpl<HeadlineMapper, Headline>
        implements HeadlineService {

    @Autowired
    private HeadlineMapper headlineMapper;

    @Autowired
    private JwtHelper jwtHelper;

    /**
     * 进行分页查询
     * 自定义SQL语句 并把结果进行封装
     *
     * @param portalVo
     * @return
     */
    @Override
    public Result findNewsPage(PortalVo portalVo) {
        // 只显示第一页的原因
        IPage<Map> iPage = new Page<>(portalVo.getPageNum(), portalVo.getPageSize());
        headlineMapper.selectMyPage(iPage, portalVo);
        List<Map> records = iPage.getRecords();
        Map data = new HashMap();
        data.put("pageData", records);
        data.put("pageNum", iPage.getCurrent());
        data.put("pageSize", iPage.getSize());
        data.put("totalPage", iPage.getPages());
        data.put("totalSize", iPage.getTotal());

        Map pageInfo = new HashMap();
        pageInfo.put("pageInfo", data);
        return Result.ok(pageInfo);
    }

    /**
     * 先查询后修改
     *
     * @param hid
     * @return
     */
    @Override
    public Result showHeadlineDetail(Integer hid) {
        Map data = headlineMapper.queryDetailMap(hid);
        Map headlineMap = new HashMap();
        headlineMap.put("headline", data);

        // 修改阅读量
        Headline headline = new Headline();
        headline.setHid((Integer) data.get("hid"));
        headline.setVersion((Integer) data.get("version"));

        headline.setPageViews((Integer) data.get("pageViews") + 1);

        headlineMapper.updateById(headline);
        return Result.ok(headlineMap);
    }

    /**
     * 要先登录
     * 发布头条
     * 1. 要补全数据
     *
     * @param headline
     * @return
     */

    @Override
    public Result publish(Headline headline, String token) {
        // 根据token查询用户信息
        int userId = Math.toIntExact((jwtHelper.getUserId(token)));

        headline.setPublisher(userId);
        headline.setPageViews(0);
        headline.setCreateTime(new Date());
        headline.setUpdateTime(new Date());

        headlineMapper.insert(headline);
        return Result.ok(null);
    }

    /**
     * 修改头条数据
     * 1. 根据hid查询数据的version(乐观锁)
     * 2. 修改文章的修改时间
     *
     * @param headline
     * @return
     */
    @Override
    public Result updateData(Headline headline) {
        Integer version = headlineMapper.selectById(headline.getHid()).getVersion();

        headline.setVersion(version);
        headline.setUpdateTime(new Date());

        headlineMapper.updateById(headline);
        return Result.ok(null);
    }


}




